-- UIOddAltar
-- Created by chengb Sep/17/2015
-- 奇怪的祭坛

UIOddAltar = class("UIOddAltar", function()
    return cc.Layer:create();
end);

function UIOddAltar.create(gridData)
    return UIOddAltar.new(gridData);
end

-- 按钮数量
local BTN_COUNT = 4;

-- 构造函数
function UIOddAltar:ctor(gridData)
    -- 初始化
    self:setName("UIOddAltar");
    local node = cc.CSLoader:createNode("layout/dungeon/special/OddAltar.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.gridData  = gridData;

    -- 点击过的按钮数据
    self.clickData = {};

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIOddAltar:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIOddAltar:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIOddAltar 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIOddAltar:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIOddAltar");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册数字按钮点击事件
    self.buttons = {};
    for i = 1, BTN_COUNT do
        local btn = findChildByName(node, "CT/btn"..i);
        table.insert(self.buttons, btn);
        local function onOkClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                AudioM.playFx("button_select");
                table.insert(self.clickData, i);
                btn:setTouchEnabled(false);
                btn:setBright(false);

                -- 检测密码
                self:tryActiveAltar();
            end
        end
        AddTouchEventListener(btn, onOkClick);

        local textLabel = findChildByName(btn, "text");
        TextStyleM.setTextStyle(textLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
        textLabel:setString(getLocStr("btn_text_odd_altar"));
    end
end

-- 重绘
function UIOddAltar:redraw()
    -- 查询建筑配置
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");
    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    titleLabel:setString(buildingInfo["title"]);

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    iconImg:loadTexture(iconPath);
    local iconSize = iconImg:getContentSize();
    local cfgOffsetY = buildingInfo["icon_offset_y"];
    if type(cfgOffsetY) ~= "number" then
        cfgOffsetY = 0;
    end

    iconImg:setPositionY(iconImg:getPositionY() + cfgOffsetY);
    iconImg:setScale(1.3);

    TextStyleM.setArtWordTitleStyle(titleLabel);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);

    applyString(descLabel, buildingInfo["dialog"]);
end

-- 重置按钮状态
function UIOddAltar:resetButtonsState()
    for _, btn in pairs(self.buttons) do
        btn:setTouchEnabled(true);
        btn:setBright(true);
    end
end

-- 尝试激活祭坛
function UIOddAltar:tryActiveAltar()
    -- 获取密码
    local password = FormulaM.invoke("ODD_ALTAR_PASSWORD");
    local canContinue = true;
    local len = #self.clickData;
    for i = 1, len do
        -- 只要有一个号码不正确就需要重置
        if self.clickData[i] ~= password[i] then
            canContinue = false;
        end
    end

    if not canContinue then
        -- 提示激活失败
        if len == 1 then
            alert(getLocStr("odd_altar_open_failed"));
        else
            alert(getLocStr("odd_altar_open_failed2"));
        end

        -- 按钮重置
        self.clickData = {};
        self:resetButtonsState();
        AudioM.playFx("slime_gain_failed");
        return;
    end

    -- 判断是否点击了所有的按钮
    if #self.clickData ~= #password then
        -- 还有按钮未被点击，让玩家继续
        return;
    end

    -- 所有号码都正确

    -- 提示激活成功
    alert(getLocStr("odd_altar_open_succ"));

    -- 标记祭坛激活中
    self.gridData.activing = true;

    -- 抛出祭坛激活的事件
    local pos = self.gridData:getPos();
    EventMgr.fire(event.ODD_ALTAR_ACTIVE, { ["pos"] = pos });

    -- 关闭当前界面
    UIMgr.getCurrentScene():removeFormByName("UIOddAltar");
end








